package com.draby.starfantasymaze.activities.main.fragments;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.draby.starfantasymaze.R;
import com.draby.starfantasymaze.dto.Tips;
import com.draby.starfantasymaze.utils.FileUtil;
import com.draby.starfantasymaze.utils.SystemUiUtil;

public class SettingFragment extends Fragment {

    // 文件存放父目录与文件名
    private String parent;
    private String data_fileName;

    // 可能用到的控件
    private ImageView iv_avatar;
    private TextView tv_nickname;
    private TextView tv_sign;
    private TextView tv_birthday;
    private Button btn_edit;
    private Button btn_tips;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_setting, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 设置文件存放父目录与文件名
        parent = getString(R.string.file_directory);
        data_fileName = getString(R.string.fileName_data);

        // 找到控件
        iv_avatar = view.findViewById(R.id.iv_avatar);
        tv_nickname = view.findViewById(R.id.tv_nickname);
        tv_sign = view.findViewById(R.id.tv_sign);
        tv_birthday = view.findViewById(R.id.tv_birthday);
        btn_edit = view.findViewById(R.id.btn_edit);
        btn_tips = view.findViewById(R.id.btn_tips);

        // 设置初始文本值
        setInitValue();

        // 游戏提示按钮点击事件
        btn_tips.setOnClickListener(v -> {
            // 显示游戏提示
            androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(getContext());
            builder.setTitle("小提示").setMessage(Tips.getInstance().getTips());
            builder.show();
        });

        // 修改生日按钮点击事件
        btn_edit.setOnClickListener(v -> {
            // 弹出日期选择控件
            DatePickerDialog dialog = new DatePickerDialog(getContext(), AlertDialog.THEME_HOLO_LIGHT);
            dialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    // 格式化生日字符串
                    @SuppressLint("DefaultLocale") String birthday = String.format("%04d-%02d-%02d", year, (month + 1), dayOfMonth);
                    // 重新设置文本显示并保存到外部文件
                    tv_birthday.setText(birthday);
                    FileUtil.setValue(parent, data_fileName, "birthday", birthday);
                }
            });
            dialog.show();
        });

        // 创建 dialog
        @SuppressLint("InflateParams") View editDialog = (LinearLayout)getLayoutInflater().inflate(R.layout.dialog_edit, null);
        // 修改昵称
        tv_nickname.setOnClickListener(v -> {
            getEditDialog(editDialog, "昵称").show();
        });
        // 修改个性签名
        tv_sign.setOnClickListener(v -> {
            getEditDialog(editDialog, "个性签名").show();
        });
        // 修改头像(暂无=w=)
    }

    /**
     * 设置初始文本值
     */
    private void setInitValue() {
        tv_nickname.setText(FileUtil.getValue(parent, data_fileName, "nickname"));
        tv_sign.setText(FileUtil.getValue(parent, data_fileName, "sign"));
        tv_birthday.setText(FileUtil.getValue(parent, data_fileName, "birthday"));
    }

    /**
     * 根据需要修改的属性获取对话框
     * @param view 对话框中自定义的视图
     * @param property 需要修改的属性名
     * @return 创建好的用于修改的 dialog
     */
    private androidx.appcompat.app.AlertDialog getEditDialog(View view, String property) {
        EditText et_modify = view.findViewById(R.id.et_modify);
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(getContext());
        builder.setTitle("修改" + property).setMessage("请输入新的" + property).setView(view);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String edit = et_modify.getText().toString();
                if ("昵称".equals(property)) {
                    tv_nickname.setText(edit);
                    FileUtil.setValue(parent, data_fileName, "nickname", edit);
                } else if ("个性签名".equals(property)) {
                    tv_sign.setText(edit);
                    FileUtil.setValue(parent, data_fileName, "sign", edit);
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                et_modify.setText("");
                // 非常重要，添加视图之后对话框消失时必须删掉视图，否则下一次打开会出错
                ((ViewGroup)view.getParent()).removeView(view);
            }
        });
        return builder.create();
    }
}
